//
//  FeelingView.swift
//  ShouChong
//
//  Created by yw on 2024/8/11.
//

import UIKit

class FeelingView: UIView {
    @IBOutlet weak var backgroundView: UIView!
    
    @IBOutlet weak var titleLabel: UILabel!
    
    @IBOutlet weak var dismayImage: UIImageView!
    
    @IBOutlet weak var normalImage: UIImageView!
    
    @IBOutlet weak var happyImage: UIImageView!
    
    @IBOutlet weak var closeBtn: UIButton!
    
    var selectAction:((Int) -> Void)?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.backgroundView.layer.cornerRadius = 20
        self.closeBtn.setImage(UIImage.systemImage(systemName: "xmark", tintColor: UIColor(named: "color_gray")!), for: .normal)
        self.dismayImage.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(imgTap(tap:))))
        self.normalImage.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(imgTap(tap:))))
        self.happyImage.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(imgTap(tap:))))
        
    }
    
    
    /// 1000 - 不高兴
    /// 1001 - 一般
    /// 1002 - 高兴
    
    @objc private func imgTap(tap:UITapGestureRecognizer) {
        let generator = UIImpactFeedbackGenerator(style: .light)
        generator.prepare()
        generator.impactOccurred()
        let tag = tap.view?.tag ?? -1
        self.selectAction?(tag)
        self.hideToView()
        
    }
    
    @IBAction func closeBtnClick(_ sender: UIButton) {
        let generator = UIImpactFeedbackGenerator(style: .light)
        generator.prepare()
        generator.impactOccurred()
        self.selectAction?(-1)
        self.hideToView()
        
    }
    
    class func createFeelingView(selectAction:@escaping (_:(Int) -> Void)) -> FeelingView {
        let feelingView = Bundle.main.loadNibNamed("FeelingView", owner:nil)?.last as! FeelingView
        feelingView.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: 200)
        feelingView.selectAction = selectAction
        return feelingView
    }
    
}
